activitiModeler.directive('actDeptUser', ['$rootScope','$http', function ($rootScope, $http) {
    return {
        restrict: 'A',
        scope: {
            model : '@ngModel'
        },
        require: 'ngModel',
        templateUrl: 'directives/act-dept-user.html',
        controller: ['$scope', '$element', function ($scope, $element) {
            $scope.treeId = 'prefix_' + Math.floor(Math.random() * (new Date()).getTime()); // 获得随机ID
        	
        	// 显示选择框
        	$scope.show = function() {
	            jQuery($element).find('.modal.fade').modal('show').css({
	                	"margin": "0px auto",
	                	"padding-top":"35px"
	            });
            };
            
            // 确定选择
            $scope.select = function() {
                $scope._ok = true;
                jQuery($element).find('.modal.fade').modal('hide');
            };
            
            // 取消选择
            $scope.cancel = function() {
            	jQuery($element).find('.modal.fade').modal('hide');
            };
            
        }],

        link: function (scope, elem, attrs, ngModelController) {
            
        	jQuery(elem).find('.ztree:eq(0)').attr('id', 'orgTree' + scope.treeId);
            scope.multiple = attrs.edMultiple == 'true';
            scope.userSelected = [];
            
            // 弹窗隐藏前赋值
            elem.find('.modal.fade').on('hidden.bs.modal', function () {
                if (scope._ok) {
                	scope._ok = false;
                    scope.updateValue();
                }
            });
            
            scope.updateTree = function() {
            	var ztree = scope.orgTree;
            	ztree.checkAllNodes(false);
            	ztree.expandAll(false);
            	scope.userSelected = [];
                if (typeof ngModelController.$viewValue !== 'undefined' && ngModelController.$viewValue.length > 0) {
                    if (scope.multiple) {
                        jQuery.each(ngModelController.$viewValue.split(','), function (index, id) {
                            var node = ztree.getNodeByParam('id', id, null);
                            if (node && node.type == 'user') {
                                ztree.checkNode(node, true, true);
                                scope.userSelected.push(node);
                            }
                        });
                    } else {
                    	var node = ztree.getNodeByParam('id', ngModelController.$viewValue, null);
                        ztree.selectNode(node, false);
                        if (node && node.type == 'user') {
                        	scope.userSelected = [node];
                        }
                    }
                    var checkedNodes = ztree.getCheckedNodes(true), checkedLen = checkedNodes.length;
                    if (checkedLen > 0) {
                        for (var i = 0; i < checkedLen; i++) {
                            ztree.expandNode(checkedNodes[i], true);
                        }
                    }
                }
            };
            
            scope.updateValue = function() {
                var users = [];
                jQuery(scope.userSelected).each(function(index, item) {
                    users.push(item.id);
                });
                
                if (scope.multiple) {
                    ngModelController.$setViewValue(users.join(','));
                } else {
                    ngModelController.$setViewValue(users.length > 0 ? users[0] : '');
                }
                var users = [];
                jQuery(scope.userSelected).each(function(index, user) {
                    users.push(user.name);
                });
                elem.find('input[type="text"]').val(users.length > 0 ? users.join(', ') : '');
            };
            
            // ztree使用
            var setting = {
                check: {
                    enable: scope.multiple
                },
                view: {
                    selectedMulti: false,
                    showIcon: function(treeId, treeNode){
                		return treeNode.type == 'user';
                	}
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: function(event, treeId, treeNode, clickFlag) {
                        if (!scope.multiple) {//单选
                        	scope.userSelected = [];
                            if (treeNode.isParent) {
                                scope.orgTree.expandNode(treeNode);
                            } else {
                                if (treeNode.type == 'user') {
                                	scope.userSelected = [treeNode];
                                }
                            }
                        }
                    },
                    onCheck : function (event, treeId, treeNode) {// 多选
                        var users = [];
                        var checkedNodes = scope.orgTree.getCheckedNodes(true);
                        jQuery(checkedNodes).each(function(index, item) {
                            if (item.type == 'user') {
                                users.push(item);
                            }
                        });
                        scope.userSelected = users;
                    }
                }
            };
            
            var key = '_userTree:' + (scope.deptId || '') + ":" + (scope.userId || '');
            var treeUrl = KISBPM.URL.getUserTree();
            $http({
            	method : 'POST',
            	url : treeUrl,
            	data : {deptId: scope.deptId},
            	cache : false
            }).success(function(response) {
            	if (response.resultCode == '1') {
            		scope.orgTree = jQuery.fn.zTree.init(jQuery("#orgTree" + scope.treeId), setting, response.data.org);
                    scope.updateTree();
                    scope.updateValue();
            	}
            });
        }
    };
}]);

activitiModeler.directive('actDeptRole', ['$rootScope','$http', function ($rootScope, $http) {
    return {
        restrict: 'A',
        scope: {
            model : '@ngModel'
        },
        require: 'ngModel',
        templateUrl: 'directives/act-dept-role.html',
        controller: ['$scope', '$element', function ($scope, $element) {
            $scope.treeId = 'prefix_' + Math.floor(Math.random() * (new Date()).getTime()); // 获得随机ID
        	
        	// 显示选择框
        	$scope.show = function() {
	            jQuery($element).find('.modal.fade').modal('show').css({
	                	"margin": "0px auto",
	                	"padding-top":"35px"
	            });
            };
            
            // 确定选择
            $scope.select = function() {
                $scope._ok = true;
                jQuery($element).find('.modal.fade').modal('hide');
            };
            
            // 取消选择
            $scope.cancel = function() {
            	jQuery($element).find('.modal.fade').modal('hide');
            };
            
        }],

        link: function (scope, elem, attrs, ngModelController) {
            
        	jQuery(elem).find('.ztree:eq(0)').attr('id', 'orgTree' + scope.treeId);
            scope.multiple = attrs.edMultiple == 'true';
            scope.roleSelected = [];
            
            // 弹窗隐藏前赋值
            elem.find('.modal.fade').on('hidden.bs.modal', function () {
                if (scope._ok) {
                	scope._ok = false;
                    scope.updateValue();
                }
            });
            
            scope.updateTree = function() {
            	var ztree = scope.orgTree;
            	ztree.checkAllNodes(false);
            	ztree.expandAll(false);
            	scope.roleSelected = [];
                if (typeof ngModelController.$viewValue !== 'undefined' && ngModelController.$viewValue.length > 0) {
                    if (scope.multiple) {
                        jQuery.each(ngModelController.$viewValue.split(','), function (index, id) {
                            var node = ztree.getNodeByParam('id', id, null);
                            if (node && node.roleId != undefined) {
                                ztree.checkNode(node, true, true);
                                scope.roleSelected.push(node);
                            }
                        });
                    } else {
                    	var node = ztree.getNodeByParam('id', ngModelController.$viewValue, null);
                        ztree.selectNode(node, false);
                        if (node && node.roleId != undefined) {
                        	scope.roleSelected = [node];
                        }
                    }
                    var checkedNodes = ztree.getCheckedNodes(true), checkedLen = checkedNodes.length;
                    if (checkedLen > 0) {
                        for (var i = 0; i < checkedLen; i++) {
                            ztree.expandNode(checkedNodes[i], true);
                        }
                    }
                }
            };
            
            scope.updateValue = function() {
                var roles = [];
                jQuery(scope.roleSelected).each(function(index, item) {
                	roles.push(item.id);
                });
                
                if (scope.multiple) {
                    ngModelController.$setViewValue(roles.join(','));
                } else {
                    ngModelController.$setViewValue(roles.length > 0 ? roles[0] : '');
                }
                var roles = [];
                jQuery(scope.roleSelected).each(function(index, role) {
                	roles.push(role.name);
                });
                elem.find('input[type="text"]').val(roles.length > 0 ? roles.join(', ') : '');
            };
            
            // ztree使用
            var setting = {
                check: {
                    enable: scope.multiple
                },
                view: {
                    selectedMulti: false,
                    showIcon: function(treeId, treeNode){
                		return treeNode.roleId != undefined;
                	}
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "id",
            			pIdKey: "parentId"
                    }
                },
                callback: {
                    onClick: function(event, treeId, treeNode, clickFlag) {// 单选
                        if (!scope.multiple) {
                        	scope.roleSelected = [];
                            if (treeNode.isParent) {
                                scope.orgTree.expandNode(treeNode);
                            } else {
                                if (treeNode.roleId != undefined) {
                                	scope.roleSelected = [treeNode];
                                }
                            }
                        }
                    },
                    onCheck : function (event, treeId, treeNode) {// 多选
                        var roles = [];
                        var checkedNodes = scope.orgTree.getCheckedNodes(true);
                        jQuery(checkedNodes).each(function(index, item) {
                            if (item.roleId != undefined) {
                            	roles.push(item);
                            }
                        });
                        scope.roleSelected = roles;
                    }
                }
            };
            
            var key = '_roleTree:' + (scope.deptId || '') + ":" + (scope.userId || '');
            var treeUrl = KISBPM.URL.getRoleTree();
            $http({
            	method : 'POST',
            	url : treeUrl,
            	data : {deptId: scope.deptId},
            	cache : false
            }).success(function(response) {
            	if (response.resultCode == '1') {
            		scope.orgTree = jQuery.fn.zTree.init(jQuery("#orgTree" + scope.treeId), setting, response.data);
                    scope.updateTree();
                    scope.updateValue();
            	}
            });
        }
    };
}]);